//
//  MYGCommunityApi.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/16.
//

import UIKit
import Moya
import HandyJSON

enum MYGCommunityListFilterType {
    case none
    case communities(ids: [String])
    case hot
    case city(city: String)
    case recommend
    case listType(type: MYGCommunityType)
    case like
    case release(type: MYGCommunityType)
}

enum MYGCommunityPublishParam {
    case title(title: String)
    case type(type: MYGCommunityType)
    case communities(ids: [String])
}

struct MYGCommunitPublushLocationInfo {
    var province = ""
    var city = ""
    var region = ""
}


enum MYGCommunityApi {
    case collect(id: String)
    case uncollect(id: String)
    case list(title: String? = nil,
              filterType: MYGCommunityListFilterType = .none,
              page: Int = 1,
              pageSize: Int = 10)
    case detail(id: Int)
    case comnunities
    case like(type:String, likeStatus: String, id: String)
    case questionList(communities_id:String) //问问列表
    case my_answersList//回答列表
    case submitAnswer(id:Int,answer:String)//提交回答
    case delMyAnswer(id:String)//删除回答
    case doctor_follow_communities //关注的社群
    case doctor_bind_communities(communities_ids:String) //医生绑定社群
    case communitiesLike(type:String, likeStatus: String, id: String)
    case question_details(community_patient_id:Int)//问问详情列表
}

extension MYGCommunityApi: MYGTargetType {
    var params: [String : Any] {
        var params: [String: Any] = [:]
        switch self {
        case .list(let title, let filterType, let page, let pageSize):
            if let title = title, title.count > 0 {
                params["title"] = title
            }
            params["page"] = String(page)
            params["pageSize"] = String(pageSize)
            switch filterType {
            case .communities(let ids):
                params["communities_id"] = ids
            case .hot:
                //TODO: hot
                break
            case .city(let city):
                params["city"] = city
            case .recommend:
                params["is_recommend"] = "1"
            case .listType(let type):
                params["type"] = String(type.rawValue)
            case .like:
                params["is_likes"] = "1"
            case .release(let type):
                params["is_release"] = "1"
                params["type"] = String(type.rawValue)
            default:
                break
            }
        case .like(let type, let likeStatus, let id):
            params["like_type"] = type
            params["like_status"] = likeStatus
            params["id"] = id
        case .collect(let id):
            params["type"] = "10"
            params["id"] = id
        case .uncollect(let id):
            params["type"] = "10"
            params["id"] = id
        case .questionList(let communities_id):
            params["communities_id"] = communities_id
        case .my_answersList,.doctor_follow_communities:
            break
        case let .submitAnswer(id, answer):
            params["community_patient_id"] = id
            params["answer"] = answer
        case .detail(let id):
            params["id"] = id
        case .delMyAnswer(let id):
            params["id"] = id
        case let .doctor_bind_communities(communities_ids):
            params["communities_ids"] = communities_ids
        case let .communitiesLike(type, likeStatus, id):
            params["like_type"] = type
            params["like_status"] = likeStatus
            params["id"] = id
        case let .question_details(community_patient_id):
            params["community_patient_id"] = community_patient_id
        default: break
        }
        return params
    }
    
    var path: String {
        switch self {
        case .comnunities:
            "/api/community/communities"
        case .list:
            "/api/community/communities_list"
        case .detail:
            "/api/community/my_answers_details"
        case .collect:
            "/api/community/collect"
        case .uncollect:
            "/api/community/uncollect"
        case .like:
            "/api/like/submit"
        case .questionList:
            "/api/community/question_list"
        case .my_answersList:
            "/api/community/my_answers"
        case .submitAnswer:
            "/api/community/question_details_answer"
        case .delMyAnswer:
            "/api/community/my_answers_del"
        case .doctor_follow_communities:
            "/api/community/doctor_follow_communities"
        case .doctor_bind_communities:
            "/api/community/doctor_bind_communities"
        case .communitiesLike:
            "/api/community/like"
        case .question_details:
            "/api/community/question_details"
        }
    }
    
    var baseURL: URL {
        switch self {
        case .list,.detail,.questionList,.my_answersList,.submitAnswer,.delMyAnswer,.comnunities,.collect,.question_details,.doctor_follow_communities,.doctor_bind_communities,.communitiesLike,.like:
            return  URL(string: BaseUrl)!
        default:
            return  URL(string: Url)!
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .detail, .collect, .uncollect, .like, .questionList,.my_answersList,.submitAnswer,.delMyAnswer,.comnunities,.doctor_follow_communities,.doctor_bind_communities,.communitiesLike,.question_details:
            .get
        case .list:
            .post
        }
    }
    
    var isServer: Int {
        1
    }
}
